package com.dimonho.chinesegeo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @author 敖癸
 * @date 2021/8/20 - 17:42
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GetDataRunner implements ApplicationRunner {

    private final GeoAtlasClient geoAtlasClient;
    private final ApplicationContext applicationContext;
    private static final Set<Integer> adCodes = CollUtil.newHashSet(100000);
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String file = FileUtil.getTmpDirPath() + "chinesgeo.json";
        List<String> outfile = args.getOptionValues("outfile");
        if (CollUtil.isNotEmpty(outfile)) {
            file = outfile.get(0);
        }
        ChineseGeo chineseGeo = new ChineseGeo()
            .setName("中华人民共和国")
            .setLevel("country")
            .setAdCode(100000);
        chineseGeo.setChildren(getData(chineseGeo));
        FileUtil.writeString(JSONUtil.toJsonPrettyStr(chineseGeo), file, "utf-8");
        log.info("outfile=【{}】写入成功", file);
        SpringApplication.exit(applicationContext);
    }

    private List<ChineseGeo> getData(ChineseGeo parentGeo) {
        String params = parentGeo.getAdCode() + "_full";
        JSONObject chinaGeo = geoAtlasClient.getChinaGeo(params);
        JSONArray features = chinaGeo.getJSONArray("features");
        List<JSONObject> featuresObjs = features.toList(JSONObject.class);
        List<ChineseGeo> chainGeos = new ArrayList<>();
        for(JSONObject feature: featuresObjs){
            JSONObject properties = feature.getJSONObject("properties");
            Integer adCode = properties.getInt("adcode");
            if (adCodes.contains(adCode)){
                continue;
            }
            adCodes.add(adCode);
            String name = properties.getStr("name");
            Integer childrenNum = properties.getInt("childrenNum", 0);
            String level = properties.getStr("level", "country");
            JSONObject parent = properties.getJSONObject("parent");
            Integer parentCode = 0;
            if (parent != null) {
                parentCode = parent.getInt("adcode");
            }
            List<Integer> acroutes = (List) properties.get("acroutes");

            ChineseGeo chineseGeo = new ChineseGeo()
                .setName(name)
                .setParent(parentCode)
                .setLevel(level)
                .setAdCode(adCode)
                .setAcRoutes(acroutes);
            if (childrenNum > 0) {
                chineseGeo.setChildren(getData(chineseGeo));
            }
            chainGeos.add(chineseGeo);
        }
        return chainGeos;
    }

}
